package com.bluesky.plum.uimodels.render.swing.components;

import java.util.List;

import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.bluesky.plum.uimodels.standard.components.ListBox;
import com.bluesky.plum.uimodels.standard.components.listbox.SelectionMode;

public class SListBox extends ListBox {

	private JList list;

	public SListBox() {
		super();

		list = new JList();
		// list.setEditable(false);

		list.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {

				if (getSelectChangeListener() != null)
					getSelectChangeListener().process();
			}
		});
		// list.setListData(options.toArray());

	}

	@Override
	public void setObjects(List objects) {
		super.setObjects(objects);

		list.setListData(this.data.toArray());

	}

	@Override
	public void addObject(Object t) {

		super.addObject(t);

		list.setListData(this.data.toArray());
	}

	@Override
	public Object getNativeComponent() {

		return list;
	}

	@Override
	public int getSelectedIndex() {

		return list.getSelectedIndex();
	}

	@Override
	public int[] getSelectedIndices() {

		return list.getSelectedIndices();
	}

	@Override
	public void setSelectedIndex(int index) {
		list.setSelectedIndex(index);

	}

	@Override
	public void setSelectedIndices(int[] indices) {

		list.setSelectedIndices(indices);
	}

	@Override
	public void setSelectionMode(SelectionMode selectionMode) {

		super.setSelectionMode(selectionMode);
		if (selectionMode == SelectionMode.Single)
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		else
			list
					.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	}

}
